﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web
{
    public partial class Main : System.Web.UI.Page
    {
        protected SessionUserInfo UserInfo
        {
            get
            {
                if (Session[SessionConstant.SESSION_USERINFO] == null)
                {
                    UtilitiesTool.RebuildSessionUserInfo();
                }
                return Session[SessionConstant.SESSION_USERINFO] == null ? null : (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            }
        }

        private string AppID
        {
            get
            {
                return ConfigurationManager.AppSettings["AppId"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ////根据角色跳转到对应主界面
            RouteFontPage();
        }

        /// <summary>
        /// 首页跳转。
        /// </summary>
        private void RouteFontPage()
        {
            SysApplication appconfig = Application[ApplicationConstant.APPLICATION_SETTING] as SysApplication;
            if (UserInfo == null)
            {
                Response.Redirect("~/login.aspx");
            }
            if (appconfig != null &&
                appconfig.SiteConfig != null &&
                appconfig.SiteConfig.FontPageConfig != null &&
                appconfig.SiteConfig.FontPageConfig.FontPages.Count > 0)
            {
                var dicFontPages = appconfig.SiteConfig.FontPageConfig.FontPages;
                string pagePage = dicFontPages.ContainsKey(UserInfo.RoleId) ? dicFontPages[UserInfo.RoleId] : string.Empty;
                if (string.IsNullOrEmpty(pagePage))
                {
                    GetOldFontPagesRoute();
                }
                else
                {
                    Response.Redirect(pagePage);
                }
            }
            else
            {
                GetOldFontPagesRoute();
            }
        }

        private void GetOldFontPagesRoute()
        {
            if (UserInfo == null)
            {
                CommonDialog.ShowAndBack("登录失败！");

            }
            else
            {
                ltrLoginUser.Text = UserInfo.UserName;
                ltrUserName.Text = UserInfo.UserName;
                switch (UserInfo.RoleType)
                {
                    case "User":
                        ////企业用户
                        Response.Redirect("Main/UserMain.aspx");
                        break;
                    case "Manager":
                        ////主管部门
                        //if (AppID == "95")
                        //{
                        //    Response.Redirect("Expert/ExpertManagerMain.aspx");
                        //}
                        //else if (UserInfo.RoleId != "80")
                        //{
                        //    if (UserInfo.RoleId == "76" || UserInfo.RoleId == "86" || UserInfo.RoleId == "87" || UserInfo.RoleId == "96")
                        //    {
                        //        Response.Redirect("Person/PersonEmpStatistic2.aspx");
                        //    }
                        //    else
                        //    {
                        Response.Redirect("Main/ManagerMain.aspx");
                        //    }
                        //}
                        break;
                    case "Expert":
                        ////专家用户
                        if (UserInfo.RoleId != "80")
                        {
                            Guid userGuid = Guid.Empty;
                            if (!string.IsNullOrEmpty(UserInfo.CorpUserGuid))
                            {
                                if (Guid.TryParse(UserInfo.CorpUserGuid, out userGuid))
                                {
                                    string url = string.Format("Person/PersonExpertInfo.aspx?empGuid={0}", userGuid);
                                    Response.Redirect(url);
                                }
                            }
                            else
                            {
                                Response.Redirect("Person/PersonExpertInfo.aspx");
                            }
                        }
                        break;
                    default:
                        break;

                }
            }
        }
    }
}